<template>
  <entity-view
    :operator-type="operatorType"
    :model="beanInfo"
    @submit="submit"
    :rules="rules"
    ref="form"
  >

    <form-item
      label="内部交易码"
      v-model="beanInfo.txnNum"
      prop="txnNum"
    ></form-item>
    <form-item
      inputType="input"
      label="业务细分"
      v-model="beanInfo.bizType "
      prop="bizType"
    ></form-item>
    <form-item
      inputType="input"
      label="交易分组"
      v-model="beanInfo.txnGroup"
      prop="txnGroup"
    ></form-item>
    <form-item
      inputType = "select"
      label="业务产品代码"
      v-model="beanInfo.bizProdCode"
      prop="bizProdCode"
      :options="bizProdCodeList"
      @change="value => selectedChange(value, 'bizProdCode')"
    ></form-item>
    <form-item
      inputType="input"
      label="交易处理服务类名"
      v-model="beanInfo.serviceName"
      prop="serviceName"
    ></form-item>
    <form-item
      inputType="input"
      label="交易超时时间"
      v-model="beanInfo.txnTimeout"
      prop="txnTimeout"
    ></form-item>
    <form-item
      inputType="input"
      label="超时交易处理码"
      v-model="beanInfo.excpTxnNum"
      prop="excpTxnNum"
    ></form-item>
     <form-item
      inputType = "select"
      label="超时服务处理方式"
      v-model="beanInfo.excpHandleFlag"
      prop="excpHandleFlag"
      :options="excpHandleFlagList"
      @change="value => selectedChange(value, 'excpHandleFlag')"
    ></form-item>
    <form-item
      inputType="input"
      label="超时处理服务类名"
      v-model="beanInfo.excpServiceName"
      prop="excpServiceName"
    ></form-item>
     <form-item
      inputType = "select"
      label="存储转发间隔类型"
      v-model="beanInfo.safGapType"
      prop="safGapType"
      :options="safGapTypeList"
      @change="value => selectedChange(value, 'safGapType')"
    ></form-item>
    <form-item
      inputType="input"
      label="超时重试次数"
      v-model="beanInfo.repeatNum"
      prop="repeatNum"
    ></form-item>
    <form-item
      inputType="input"
      label="超时重试时间间隔"
      v-model="beanInfo.repeatTime"
      prop="repeatTime"
    ></form-item>
    <form-item
      inputType = "select"
      label="交易支持标志"
      v-model="beanInfo.supportFlag"
      prop="supportFlag"
      :options="supportFlagList"
      @change="value => selectedChange(value, 'supportFlag')"
    ></form-item>
    <form-item
      inputType="input"
      label="交易支持开始时间"
      v-model="beanInfo.supportBeginTime"
      prop="supportBeginTime"
    ></form-item>
    <form-item
      inputType="input"
      label="交易支持截止时间"
      v-model="beanInfo.supportEndTime"
      prop="supportEndTime"
    ></form-item>
    <form-item
      inputType = "select"
      label="流水表登记标识"
      v-model="beanInfo.txnRegisterFlag"
      prop="txnRegisterFlag"
      :options="txnRegisterFlagList"
      @change="value => selectedChange(value, 'txnRegisterFlag')"
    ></form-item>
    <form-item
      inputType = "select"
      label="原始报文登记标识"
      v-model="beanInfo.msgRegisterFlag"
      prop="msgRegisterFlag"
      :options="msgRegisterFlagList"
      @change="value => selectedChange(value, 'msgRegisterFlag')"
    ></form-item>
    <form-item
      inputType = "select"
      label="验签标志"
      v-model="beanInfo.signFlag"
      prop="signFlag"
      :options="signFlagList"
      @change="value => selectedChange(value, 'signFlag')"
    ></form-item>
    <form-item
      inputType = "select"
      label="转加密标志"
      v-model="beanInfo.pinFlag"
      prop="pinFlag"
      :options="pinFlagList"
      @change="value => selectedChange(value, 'pinFlag')"
    ></form-item>
    <form-item
      inputType="input"
      label="交易描述"
      v-model="beanInfo.txnName"
      prop="txnName"
    ></form-item>
  </entity-view>
</template>
<script>
import EntityView from "../../../../components/common/entityView/EntityView.vue";
import FormItem from "../../../../components/common/entityView/FormItem.vue";
import moment from "moment";
moment.locale('zh-cn');

export default {
  name: "pTxnCfgOperation",
  components: {
    EntityView,
    FormItem,
  },
  data: function () {
    return {
      showSearch: true,
      operatorType: "add",
      beanInfo: {
        txnNum:"",
        bizType:"",
        txnGroup:"",
        bizProdCode:"",
        serviceName:"",
        txnTimeout:"",
        excpTxnNum:"",
        excpHandleFlag:"",
        excpServiceName:"",
        safGapType:"",
        repeatNum:"",
        repeatTime:"",
        supportFlag:"",
        supportBeginTime:"",
        supportEndTime:"",
        txnRegisterFlag:"",
        msgRegisterFlag:"",
        signFlag:"",
        pinFlag:"",
        txnName:"",
        createTime:"",
        updateTime:"",
      },
      rules: {
        txnNum: [
          { required: true, message: "必填", trigger: "blur" },
        ],
        bizType: [
          { required: true, message: "必填", trigger: "blur" },
        ],
        txnGroup: [{ required: true, message: "必填", trigger: "blur" },],
        bizProdCode: [
          { required: true, message: "必填", trigger: "blur" },
        ],
        serviceName: [
          { required: true, message: "必填", trigger: "blur" },
          { max: 64, message: "不超过64位字符", trigger: "focus" },
        ],
        txnTimeout: [
          { required: true, message: "必填", trigger: "blur" },
          { reg: '^[1-9][0-9]{1,2}$', message: "请检查格式", trigger: "blur" },
        ],
        excpTxnNum: [
          { max: 8, message: "不超过8位字符", trigger: "blur" },
        ],
         repeatNum: [
          { max: 2, message: "不超过2位字符", trigger: "blur" },
          { reg: '^[1-9][0-9]{0,7}$', message: "请检查格式", trigger: "focus" },
        ],
         repeatTime: [
          { max: 2, message: "不超过2位字符", trigger: "blur" },
          { reg: '^[1-9][0-9]{1,2}$', message: "请检查格式", trigger: "blur" },
        ],
        supportFlag: [
          { required: true, message: "必填", trigger: "blur" },
        ],
        supportBeginTime: [
          { required: true, pattern :/^\d{1,6}$/,message: "请检查格式", trigger: "blur" },
        ],
         supportEndTime:[
          { required: true, pattern :/^\d{1,6}$/, message: "请检查格式", trigger: "blur" },
        ],
        msgRegisterFlag:[
          { required: true, message: "必填", trigger: "blur" },
        ],
        signFlag:[
          { required: true, message: "必填", trigger: "blur" },
        ],
        pinFlag:[
          { required: true, message: "必填", trigger: "blur" },
        ],
        txnName:[
          { required: true, message: "必填", trigger: "blur" },
        ],
      },
      bizProdCodeList:[
        {value:"00",name:"基础公共"},
        {value:"01",name:"刷卡支付"},
        {value:"02",name:"付款扫码(主扫)"},
        {value:"03",name:"收款扫码(被扫)"},
        {value:"04",name:"信用卡分期"},
        {value:"05",name:"银行卡支付"},
        {value:"06",name:"XX手机银行支付"},
        {value:"07",name:"微信支付"},
        {value:"08",name:"支付宝支付"},
        {value:"09",name:"银联云闪付"},
        {value:"10",name:"代付POS"},
      ],
      excpHandleFlagList:[
        {value:"0",name:"同步处理"},
        {value:"1",name:"异步处理"},
      ],
      safGapTypeList:[
        {value:"0",name:"等时长间隔"},
        {value:"1",name:"等幂间隔"},
      ],
      supportFlagList:[
        {value:"Y",name:"支持"},
        {value:"N",name:"不支持"},
      ],
      txnRegisterFlagList:[
        {value:"P",name:"登记金融流水表"},
        {value:"T",name:"登记非金融流水表"},
        {value:"M",name:"登记管理流水表"},
        {value:"N",name:"不登记流水"},
      ],
      msgRegisterFlagList:[
        {value:"Y",name:"登记"},
        {value:"N",name:"不登记"},
      ],
      signFlagList:[
        {value:"Y",name:"验签/MAC"},
        {value:"N",name:"不验签/MAC"},
      ],
      pinFlagList:[
        {value:"Y",name:"转加密"},
        {value:"N",name:"不转加密"},
      ],
    };
  },
  methods: {
    selectedChange(value,param) {
      this.beanInfo[param] = value;
      console.log(param,this.beanInfo[param]);
    },
    async submit() {
      let res;
      switch (this.operatorType) {
        case "add":
          res = await this.add();
          break;
        case "edit":
          res = await this.edit();
          break;
        case "view":
          res = true;
          break;
        default:
          break;
      }
      return res;
    },
    async add() {
      try {
        const res = await this.$refs.form.validate();
        console.log(res);
        if (res) {
          const params = this.beanInfo;
          const res = await this.$post("/admin/igas/pTxnCfg/edit", params);
          if (res.status === 200) {
            this.$message.success("新增成功！");
            return true;
          } else {
            this.$message.error(res.data.message);
            return false;
          }
        }
      } catch (e) {
        console.error(e);
      }
    },
    async edit() {
      const res = await this.$refs.form.validate();
      if (res) {
        const params = this.beanInfo;
        console.log(params);
        const res = await this.$post(
          "/admin/igas/pTxnCfg/edit",
          params
        );
        if (res.status === 200) {
          this.$message.success("保存成功！");
          return true;
        } else {
          this.$message.error(res.data.message);
          return false;
        }
      }
    },
    resetFields() {
      return this.$refs.form.resetFields();
    },
    setOperatorType(type) {
      this.operatorType = type;
    },
    setBeanInfo(beanInfo) {
      this.beanInfo = beanInfo;
      this.getOne();
    },
    async getOne() {
      try {
        const res = await this.$get("/admin/igas/pTxnCfg/getOne", {
          bizProdCode:this.beanInfo.bizProdCode,
          bizType:this.beanInfo.bizType,
          txnGroup:this.beanInfo.txnGroup,
          txnNum:this.beanInfo.txnNum,
        });
        this.beanInfo = res.data;
        this.beanInfo.txnTimeout = this.beanInfo.txnTimeout  + ""
      } catch (e) {
        console.error(e);
      }
    },
  },
};
</script>
